package com.jyu.work.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jyu.pojo.DTO.UserLoginDTO;
import com.jyu.common.exception.BaseException;
import com.jyu.work.mapper.UserMapper;
import com.jyu.pojo.entity.User;
import com.jyu.work.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;
    // 用户注册
    @Override
    public void register(User user) {
        // 存入的用户是否存在
        String userName = user.getUserName();

        User exited = userMapper.selectOne(new LambdaQueryWrapper<User>().eq( User::getUserName, userName));

        // 在就不用添加了，抛出异常直接返回
        if( exited != null) {
            throw new BaseException(user.getUserName()+"已存在,");
        }
        // 不在，新人。 添加数据库
        userMapper.insert(user);
    }

    @Override
    public User login(UserLoginDTO loginDTO) {

        String username = loginDTO.getUsername();
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<User>().eq(User::getUserName, username);

        User User = userMapper.selectOne(queryWrapper);

        if(User == null) {
            throw new BaseException("用户不存在");
        }


        return User;
    }
}
